package swu.cx.defineviewgroup

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.TextView

class FlowLayout:ViewGroup {
    private var allLineChildren:MutableList<MutableList<View>>? = null
    private var lineHeights:MutableList<Int>? = null
    private val space = 30
    constructor(context: Context, attr: AttributeSet):super(context,attr){}

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        allLineChildren = mutableListOf()
        lineHeights = mutableListOf()
        val parentWidth = MeasureSpec.getSize(widthMeasureSpec)
        val parentHeight = MeasureSpec.getSize(heightMeasureSpec)
        val parentWidhtMode = MeasureSpec.getMode(widthMeasureSpec)
        val parentHightMode = MeasureSpec.getMode(heightMeasureSpec)
        var resultWidth = 0
        var resultHeight = space
        //记录行宽、高
        var lineWidth = space
        var lineHeight = 0
        var lineChildren = mutableListOf<View>()
        for (i in 0 until childCount){
            val child = getChildAt(i)
            val lp = child.layoutParams
            val childWidthSpec = getChildMeasureSpec(widthMeasureSpec,2*space,lp.width)
            val childHeightSpec = getChildMeasureSpec(heightMeasureSpec,2*space,lp.height)
            child.measure(childWidthSpec,childHeightSpec)
            if (child.measuredWidth+lineWidth>parentWidth){
                allLineChildren!!.add(lineChildren)
                lineHeights!!.add(lineHeight)
                resultWidth = Math.max(resultWidth,lineWidth)
                resultHeight+=lineHeight+space
                lineWidth = 2*space+child.measuredWidth
                lineHeight = child.measuredHeight
                lineChildren = mutableListOf()
                lineChildren.add(child)
            }else{
                lineWidth+=child.measuredWidth+space
                lineHeight = Math.max(lineHeight,child.measuredHeight)
                lineChildren.add(child)
            }
        }
        if (lineChildren.isNotEmpty()){
            allLineChildren?.add(lineChildren)
            lineHeights?.add(lineHeight)
            resultWidth = Math.max(resultWidth,lineWidth)
            resultHeight+=lineHeight+space
        }
        resultWidth = if (parentWidhtMode==MeasureSpec.EXACTLY) parentWidth else resultWidth
        resultHeight = if (parentHightMode==MeasureSpec.EXACTLY) parentHeight else resultHeight
        setMeasuredDimension(resultWidth,resultHeight)
    }
    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        var left = space
        var top = space
        for (i in 0 until allLineChildren!!.size){
            val lineChildren = allLineChildren!![i]
            val lineHeight = lineHeights!![i]
            for (j in 0 until lineChildren.size){
                val child = lineChildren[j]
                child.layout(left,top,left+child.measuredWidth,top+child.measuredHeight)
                left+=child.measuredWidth+space
            }
            left=space
            top+=lineHeight+space
        }
    }
}